package tcm.com.gistone.controller;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import tcm.com.gistone.database.config.GetBySqlMapper;
import tcm.com.gistone.database.entity.*;
import tcm.com.gistone.database.mapper.*;
import tcm.com.gistone.util.ClientUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by qiang on 2018/1/16.
 */
@Component
public class ModifyWord {
    private final static org.slf4j.Logger logger = LoggerFactory.getLogger(ClientUtil.class);
    @Autowired
    private SectionMapper sm;
    @Autowired
    private WordMapper wm;
    @Autowired
    private WsRelationMapper wsm;
    @Autowired
    private SpecialMapper spm;
    @Autowired
    private ThemeMapper tm;
    @Autowired
    private ConfidenceMapper cm;
    @Autowired
    private WordsRelationMapper wordsRelationMapper;
    @Autowired
    private GetBySqlMapper gm;

    /*添加段落与主题词或者别名关系*/
    @Async
    public void addWsRelationByWord(long wordId) throws InterruptedException {
        Word word = wm.selectByPrimaryKey(wordId);
        if(word==null){
            logger.info("添加失败");
            return;
        }
        logger.info("开始添加词：" + word.getWord() + "的词和段落之间关系");
        long type1 = word.getThemeType();
        long specialId = word.getSpecialId();
        Long wid1 = word.getParentId();
        List<Section> list = sm.selectBySpecial(specialId);/*专题下所有段落*/
        String sql1 = "select * from tb_word where parent_id = " + wordId;
        List<Map> list1 = gm.findRecords(sql1);/*查询主题词和别名*/
        List<String> words = new ArrayList<>();
        for (Map map : list1) {
            words.add(map.get("word").toString());
        }
        int flag = 0;
        List<WsRelation> list2 = new ArrayList<>();
        for (Section sec : list) {
            String text = sec.getSectionContent();/*段落内容*/
            int num1 = 0;/*匹配次数*/
            for (String str : words) {
                if (text.indexOf(str) != -1) {
                     /*查询包含当前主题词的其他主题词或别名*/
                    String sql = "select word from tb_word where special_id = " + specialId + " and word != '" + str + "' and word like '%" + str + "%'";
                    List<Map> list11 = gm.findRecords(sql);
                    /*如果存在，剔除其他主题词或别名*/
                    if (list11.size() > 0) {
                        for (Map map1 : list11) {
                            text = text.replace(map1.get("word").toString(), "");
                        }
                    }
                    flag = text.indexOf(str);
                    while (flag != -1) {
                        num1++;
                        flag = text.indexOf(str, flag + 1);
                    }
                }
            }
            /*匹配成功*/
            if (num1 > 0) {
                long tid = sec.getSectionId();
                long id = sec.getBookId();
                WsRelation wsRelation = new WsRelation();
                wsRelation.setWordId(wid1);
                wsRelation.setSectionId(tid);
                wsRelation.setWordNum(num1);
                wsRelation.setBookId(id);
                wsRelation.setWordType(type1);
                list2.add(wsRelation);
            }
        }
        if (list2.size() > 0) {
            wsm.insertBatch(list2);
        }
        logger.info("完成添加词：" + word.getWord() + "，词和段落之间知识关系");
        addWordRelation(wordId);
    }

    /*添加主题词间关系*/
    public void addWordRelation(long wid1) {
        Word word = wm.selectByPrimaryKey(wid1);
        logger.info("开始添加词：" + word.getWord() + "，主题词之间关系");
        List<WsRelation> list = wsm.selectByWId(wid1);
        List<WordsRelation> list3 = new ArrayList<>();/*新添加主题词的段落匹配数据*/
        List<Confidence> list4 = new ArrayList<>();
        for (WsRelation wsRelation : list) {
            int num1 = wsRelation.getWordNum();
            long type1 = wsRelation.getWordType();
            long secId = wsRelation.getSectionId();
            long id = wsRelation.getBookId();
            String secType = wsRelation.getSecType();
            List<WsRelation> list1 = wsm.selectBySId(secId);
            if (list1.size() > 1) {
                for (WsRelation wsRelation1 : list1) {
                    long type2 = wsRelation1.getWordType();
                    long wid2 = wsRelation1.getWordId();
                    int num2 = wsRelation1.getWordNum();
                    int num = num1 < num2 ? num1 : num2;
                    if (type1 < type2) {
                        WordsRelation wordsRelation = new WordsRelation();
                        wordsRelation.setBookId(id);
                        wordsRelation.setSecId(secId);
                        wordsRelation.setWordId(wid1);
                        wordsRelation.setAnoWordId(wid2);
                        wordsRelation.setWordType(type1);
                        wordsRelation.setAnoWordType(type2);
                        wordsRelation.setNum(num);
                        wordsRelation.setSecType(secType);
                        list3.add(wordsRelation);
                        Confidence confidence = new Confidence();
                        confidence.setWordId(wid1);
                        confidence.setAnoWordId(wid2);
                        confidence.setValue((float) num / num1);
                        Confidence confidence1 = new Confidence();
                        confidence1.setWordId(wid2);
                        confidence1.setAnoWordId(wid1);
                        confidence1.setValue((float) num / num2);
                        list4.add(confidence);
                        list4.add(confidence1);
                    } else if (type1 > type2) {
                        WordsRelation wordsRelation = new WordsRelation();
                        wordsRelation.setBookId(id);
                        wordsRelation.setSecId(secId);
                        wordsRelation.setWordId(wid2);
                        wordsRelation.setAnoWordId(wid1);
                        wordsRelation.setWordType(type2);
                        wordsRelation.setAnoWordType(type1);
                        wordsRelation.setNum(num);
                        wordsRelation.setSecType(secType);
                        list3.add(wordsRelation);
                        Confidence confidence = new Confidence();
                        confidence.setWordId(wid1);
                        confidence.setAnoWordId(wid2);
                        confidence.setValue((float) num / num1);
                        Confidence confidence1 = new Confidence();
                        confidence1.setWordId(wid2);
                        confidence1.setAnoWordId(wid1);
                        confidence1.setValue((float) num / num2);
                        list4.add(confidence);
                        list4.add(confidence1);
                    }
                }
            }
        }
        if (list3.size() > 0) {
            wordsRelationMapper.insertBatch(list3);
        }
        if (list4.size() > 0) {
            cm.insertBatch(list4);
        }
        logger.info("完成添加词：" + word.getWord() + "，主题词之间关系");
        Special special = spm.selectByPrimaryKey(word.getSpecialId());
        if (special.getStatus() != 1) {
            special.setStatus(1);
            spm.updateByPrimaryKey(special);
        }
    }
}
